<?php

// phpMyRealty 3
//
// File Name: images.php
// File Location : ./includes/functions/
//
// Copyright (c)2009 phpMyRealty.com
//
// e-mail: support@phpMyRealty.com

// ----------------------------------------------------------------------------
// banner_upload($folder,$id,$image,$max_width,$max_height,$max_size)
//
// uploads the gif or jpg banner image with image and file size 
// validation
//
// $folder - folder name to upload the image to
// $id - listing ID to upload the banner for
// $image - uploaded image temp name
// $max_width - maximum width in pixels
// $max_height - maximum height in pixels
// $max_size - maximum file size in bytes
//

function banner_upload ( $folder, $id, $image, $max_width, $max_height, $max_size ) {

 global $db;
 ini_set( 'gd.jpeg_ignore_warning', 1 );

 // If image exist we start
 if ( $image ) {
  // Set new temp file permissions to 644
  chmod ( $image, 0644 ) or error ('Critical Error', 'CHMOD function is unavailable, please, contact your host support to fix this.');

  $image_size = getimagesize ( $image );
  $image_filesize = filesize ( $image );

  if ($image_size[2] == 1)
   $ext = 'gif';
  if ($image_size[2] == 2)
   $ext = 'jpg';

  // Check if the uploaded image size and type 
  // is okay
  if ( ( $image_size[0] <= $max_width ) and ( $image_size[1] <= $max_height ) and ( $image_filesize < $max_size ) and ( $image_size[2] == 1 || $image_size[2] == 2 ) ) {
   // Remove old jpg, gif files of this user if exist
   if (file_exists( PATH . '/' . $folder . '/' . $id . '.jpg'))
    unlink ( PATH . '/' . $folder . '/' . $id . '.jpg' );
   if (file_exists( PATH . '/' . $folder . '/' . $id . '.gif'))
    unlink ( PATH . '/' . $folder . '/' . $id . '.gif' );

   // Copy the uploaded fullsize image into the $folder
   move_uploaded_file ( $image, PATH . '/' . $folder . '/' . $id . '.' . $ext ) or error ( 'Critical Error', 'Can\'t upload banner file into the ' . $folder . ', please, check folder permissions.');

   // Change file permissions to 644
   chmod ( PATH . '/' . $folder . '/' . $id . '.' . $ext , 0644 );

   $upload_banner = TRUE;

   // Updating databases
   if ($folder == 'banners') {
    $sql = 'UPDATE ' . PROPERTIES_TABLE . ' SET banner = "' . $id . '.' . $ext . '" WHERE id = "' . $id . '" LIMIT 1';
    $db->query($sql) or error ('Critical Error', mysql_error());
   }
  }
  else $upload_banner = FALSE;

 }
 else $upload_banner = FALSE;

 return $upload_banner;

}


// ----------------------------------------------------------------------------
// banner_remove($folder,$id)
//
// removes the banner image from the folder
//
// $folder - folder name to remove the image from
// $id - listing ID 
//

function banner_remove($folder, $id) {

 global $db;

 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '.jpg' ))
  unlink (PATH . '/' . $folder . '/' . $id . '.jpg' );
 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '.gif' ))
  unlink (PATH . '/' . $folder . '/' . $id . '.gif' );

 if ($folder == 'banners') {
  $sql = 'UPDATE ' . PROPERTIES_TABLE . ' SET banner = "" WHERE id = "' . $id . '" LIMIT 1';
  $db->query($sql) or error ('Critical Error', mysql_error());
 }

}

// ----------------------------------------------------------------------------
// show_banner($folder,$id)
//
// creates html code for the banner
//
// $folder - folder name
// $id - listing ID 
//

function show_banner($folder, $id) {

 $output = '';

 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '.jpg' ))
  $output = '<img src="' . URL . '/' . $folder . '/' . $id . '.jpg?nocache=' . rand ( 0, 999999 ) . '" class="images" border="0" alt="" /><br />';
 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '.gif' ))
  $output = '<img src="' . URL . '/' . $folder . '/' . $id . '.gif?nocache=' . rand ( 0, 999999 ) . '" class="images" border="0" alt="" /><br />';

 return $output;

}

// ----------------------------------------------------------------------------
// upload_image($folder,$id,$image,$resampled_width,$main_width='640')
//
// uploads the jpg image with image and file size validation
// and thumbnail creation
//
// $folder - folder name to upload the image to
// $id - listing ID to upload the image for
// $image - uploaded image temp name
// $resampled_width - width to resize the thumbnail image to
// $main_width - width to resize the source image to
//

function upload_image($folder,$id,$image,$resampled_width,$main_width='640') {

  global $db;
  ini_set( 'gd.jpeg_ignore_warning', 1 );

  // If image exist we start
  if ( $image )
   {
    // Set new temp file permissions to 777
    chmod ( $image, 0644 ) or error ('Critical Error', 'CHMOD function is unavailable, please, contact your host support to fix this.');

    $image_size = getimagesize ( $image );
    $image_filesize = filesize ( $image );

    // Check if the uploaded image size and type 
    // is okay and start resampling
       if ( $image_size[2] == 2 )

	{

	 // Remove old jpg files of this user if exist
         if (file_exists( PATH . '/' . $folder . '/' . $id . '.jpg'))
	 unlink ( PATH . '/' . $folder . '/' . $id . '.jpg' );
         if (file_exists( PATH . '/' . $folder . '/' . $id . '-resampled.jpg'))
	 unlink ( PATH . '/' . $folder . '/' . $id . '-resampled.jpg' );

	 // If original image is smaller than 640 we do not change it
         if ( $image_size[0] > $main_width ) $width = $main_width; else $width = $image_size[0];

	 // Creating a new image
	 $image_new1 = imagecreatefromjpeg ( $image ) or error ('Critical Error', 'GD library is not installed, can\'t upload image');

	 // Calculating a new height of an image
         $new = imagesy ( $image_new1 ) / ( imagesx ( $image_new1 ) / $width );

         if ( function_exists ( 'imagecreatetruecolor' ) )
	  $resampled_image = imagecreateTrueColor ( $width, $new );
 	 else
	  $resampled_image = imagecreate ( $width, $new);

	 // Creating a new image on the disk
         ImageCopyResampled ( $resampled_image, $image_new1, 0, 0, 0, 0, imagesx( $resampled_image ), imagesy ( $resampled_image ), imagesx ( $image_new1 ), imagesy ( $image_new1 ) );
         Imagejpeg( $resampled_image, PATH . '/' . $folder . '/' . $id  . '.jpg' , 80);

	 // Change file permissions to 777
	 chmod ( PATH . '/' . $folder . '/' . $id . '.jpg' , 0644 );

	 // Creating a resized image using GD

	 $image_path = PATH . '/' . $folder . '/' . $id . '.jpg';

	 $image_info = getimagesize ( $image_path);

	 // Creating a new image
	 $image_new = imagecreatefromjpeg ( $image_path ) or error ('Critical Error', 'GD library is not installed, can\'t upload image');

	 // Calculating a new height of an image
         $new = imagesy ( $image_new ) / ( imagesx ( $image_new ) / $resampled_width );

         if ( function_exists ( 'imagecreatetruecolor' ) )
	  $resampled_image = imagecreateTrueColor ( $resampled_width, $new );
 	 else
	  $resampled_image = imagecreate ( $resampled_width, $new);

	 // Creating a new image on the disk
         ImageCopyResampled ( $resampled_image, $image_new, 0, 0, 0, 0, imagesx( $resampled_image ), imagesy ( $resampled_image ), imagesx ( $image_new ), imagesy ( $image_new ) );
         Imagejpeg( $resampled_image, PATH . '/' . $folder . '/' . $id . '-resampled.jpg' , 80);

         chmod (PATH . '/' . $folder . '/' . $id . '-resampled.jpg', 0644);

	 $upload = TRUE;

	 // Updating databases

	if ($folder == 'photos')
	 {
	  $sql = 'UPDATE ' . USERS_TABLE . ' SET image_uploaded = 1 WHERE id = "' . $id . '" LIMIT 1';
	  $db->query($sql) or error ('Critical Error', mysql_error());
	 }

	if ($folder == 'images')
	 {
	  $sql = 'UPDATE ' . PROPERTIES_TABLE . ' SET image_uploaded = 1 WHERE id = "' . $id . '" LIMIT 1';
	  $db->query($sql) or error ('Critical Error', mysql_error());
	 }

	}

    else $upload = FALSE;

   }

  else $upload = FALSE;

  return $upload;

 }

// ----------------------------------------------------------------------------
// show_image($folder,$id)
//
// creates html code for the listing image
//
// $folder - folder name
// $id - listing ID 
//

function show_image($folder, $id) {

 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '-resampled.jpg' ))
  $output = '<img src="' . URL . '/' . $folder . '/' . $id . '-resampled.jpg?nocache=' . rand ( 0, 999999 ) . '" class="images" border="0" alt="" /><br />';
 else
  $output = '<img src="' . URL . '/' . $folder . '/empty.jpg?nocache=' . rand ( 0, 999999 ) . '" class="images" border="0" alt="" /><br />';

 return $output;

}

// ----------------------------------------------------------------------------
// show_gallery_image($folder,$id,$description='')
//
// creates html code for the gallery image
//
// $folder - folder name
// $id - image ID 
// $description - image description
//

function show_gallery_image ( $folder, $id, $description = '') {

 global $conf;

 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '-resampled.jpg' ))
  $output = '<a href="' . URL . '/' . $folder . '/' . $id . '.jpg?nocache=' . rand ( 0, 999999 ) . '" rel="lightbox[gallery]" title="' . $description . '"><img src="' . URL . '/' . $folder . '/' . $id . '-resampled.jpg" class="images" border="0"  alt="" /></a>';
 else
  $output = '<img src="' . URL . '/' . $folder . '/empty.jpg?nocache=' . rand ( 0, 999999 ) . '" class="images" border="0" alt="" />';

 return $output;

}

// ----------------------------------------------------------------------------
// remove_image($folder,$id)
//
// removes the image from the disc
//
// $folder - folder name
// $id - image ID 
//

function remove_image($folder, $id) {

 global $db;

 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '.jpg' ))
  unlink (PATH . '/' . $folder . '/' . $id . '.jpg' );

 if ( file_exists ( PATH . '/' . $folder . '/' . $id . '-resampled.jpg' ))
  unlink (PATH . '/' . $folder . '/' . $id . '-resampled.jpg' );

 // Updating databases
 
 if ($folder == 'photos') {
  $sql = 'UPDATE ' . USERS_TABLE . ' SET image_uploaded = 0 WHERE id = "' . $id . '" LIMIT 1';
  $db->query($sql) or error ('Critical Error', mysql_error());
 }

 if ($folder == 'images') {
  $sql = 'UPDATE ' . PROPERTIES_TABLE . ' SET image_uploaded = 0 WHERE id = "' . $id . '" LIMIT 1';
  $db->query($sql) or error ('Critical Error', mysql_error());
 }

}

?>